import java.util.*;
import common.*;

public class Monitor extends Thread{
	ArrayList<Process> processes;
	
	String input;
	boolean running = true;
	
	public Monitor( ArrayList<Process> _processes ){
		processes = _processes;		// processes to manage
	}
	
	/**
	 * This will contain the scenario of simulation.
	 */
	public static void autoexec(){
		Utils.print_log( "autoexec..." );
		
		
	}
	
	/**
	 * The management monitor process. it offers a prompt to receive user input, parses the input, and dispatches the operations.
	 */
	public void run(){
		// regular prompt
		Scanner key_in = new Scanner( System.in );
		
		while( running ){
			System.out.print( "root# " );
			input = key_in.nextLine();	// read a line
			
			// parsing
			StringTokenizer input_token = new StringTokenizer( input );
			if( input_token.hasMoreTokens() ){
				String command = input_token.nextToken(); // copy: 1st token
				
				// help
				if( command.toLowerCase().equals( "help" ) ){
					// print out the help message
					System.out.println( "-monitor: [ HELP :: Message Rules ]" );
					System.out.println( "# say (sender_id) (receiver_id) (msg)  --  send a message" );
					System.out.println( " ... " );
					System.out.println();
					
				}else if( command.toLowerCase().equals( "quit" ) ){
					// stop all local processes
					System.out.println( "-monitor: Terminating..." );
					
					for( Process p : processes ){
						//p.shutdown();		// i commented it for p.shutdown() makes an error
					}
					try{
						sleep( Conf.THREAD_SLEEPTIME_MS );
					}catch( InterruptedException e ){
						Utils.print_log( "ERROR: critical error (check your code @fds983) -- Monitor.java" );
					}
					System.out.println( "-monitor: Bye!" );
					System.exit( 0 );
				}else{
					System.out.println( "-monitor: " + command + ": command not found" );
				}
			}
		}//end-of-while( running )
	}//end-of-run()
}//end-of-class
